package com.hbnu.system.controller;


import com.hbnu.system.core.base.CommonPage;
import com.hbnu.system.core.base.Rest;
import com.hbnu.system.model.context.BaseContext;
import com.hbnu.system.model.dto.UserPasswordDTO;
import com.hbnu.system.model.dto.UserSearchPageDTO;
import com.hbnu.system.model.entity.User;
import com.hbnu.system.model.vo.UserVO;
import com.hbnu.system.service.IUserService;
import com.hbnu.system.utils.MD5Util;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-05-16
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户模块")
@Slf4j
public class UserController {

    @Autowired
    private IUserService iUserService;

    @ApiOperation("新增用户")
    @PostMapping("/addUser")
    public Rest<Boolean> InsertUser(@RequestBody User user){
        boolean insert = iUserService.insertUser(user);
        return insert ? Rest.success() : Rest.failed();
    }

    @ApiOperation("通过工号得到用户信息")
    @GetMapping("/getUserByAccount")
    public Rest<List<User>> GetUserByAccount(String account){
        List<User> userByAccount = iUserService.getUserListByAccount(account);
        return Rest.success(userByAccount);
    }

    @ApiOperation("首页获取成果数据")
    @GetMapping("/getTotalAchievement")
    public Rest<List<List<Integer>>> getTotalAchievement(@RequestHeader("X-Token") String token){
        List<List<Integer>> totalAchievement = iUserService.getTotalAchievement(token);
        return Rest.success(totalAchievement);
    }

    @ApiOperation("首页获取项目数据")
    @GetMapping("/getTotalProject")
    public Rest<List<List<Integer>>> getTotalProject(@RequestHeader("X-Token") String token){
        List<List<Integer>> totalProject = iUserService.getTotalProject(token);
        return Rest.success(totalProject);
    }


    @ApiOperation("分页获取用户列表")
    @PostMapping("/getUserList")
    public Rest<CommonPage<UserVO>> getUserList(@RequestBody UserSearchPageDTO userSearchPageDTO){
        CommonPage<UserVO> userList = iUserService.getUserList(userSearchPageDTO);
        return Rest.success(userList);
    }

    @ApiOperation("删除用户")
    @GetMapping("/deleteUser")
    public Rest deleteUser(String id){
        iUserService.removeById(id);
        return Rest.success();
    }

    @ApiOperation("更新用户")
    @PutMapping("/updateUser")
    public Rest updateUser(@RequestBody User user){
        iUserService.updateUser(user);
        return Rest.success();
    }

    @ApiOperation("更新密码")
    @PostMapping("/updatePassword")
    public Rest updatePassword(@RequestBody UserPasswordDTO userPasswordDTO){
        String currentId = BaseContext.getCurrentId();
        log.info("当前用户id为：{}", currentId);

        if (!userPasswordDTO.getNewPassword().equals(userPasswordDTO.getPassword())) {
            return Rest.failed("两次密码不一致");
        }

        String encryptedPassword = MD5Util.encrypt(userPasswordDTO.getNewPassword());
        if (encryptedPassword == null) {
            return Rest.failed("密码加密失败");
        }

        userPasswordDTO.setUid(currentId);
        userPasswordDTO.setNewPassword(encryptedPassword);

        iUserService.updatePassword(userPasswordDTO);

        return Rest.success("密码更新成功");
    }

    @ApiOperation("同步中间数据库人员信息至主数据库")
    @GetMapping("/syncUser")
    public Rest syncUser(){
        iUserService.syncAllUser();
        return Rest.success();
    }

    @ApiOperation("批量加密所有用户密码")
    @GetMapping("/batchEncryptPassword")
    public Rest<Integer> batchEncryptPassword(){
        int count = iUserService.batchEncryptPassword();
        return Rest.success(count, "成功加密" + count + "个用户密码");
    }


}
